/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * 105, S.S Paradise Apartment, Munnireddy Layout, Horamavu, Banaswadi, Bangalore, India.
 */
package org.or5e.api.webcam;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.Processor;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MyJFrame extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	WebCamera wc;
    JButton start = new JButton("Start");
    JButton stop = new JButton("Stop");

    public MyJFrame(String title, WebCamera wc) {
        super(title);
        this.wc = wc;
        setSize(400, 400);
        Container container = getContentPane();
        setLayout(new BorderLayout());
        Component cc;
        Component vc;
        stop.addActionListener(this);
        start.addActionListener(this);
        Processor p = wc.getProcessor("vfw:WebCam Vista/Live! Cam Chat (VFW):0");
        if ((vc = p.getVisualComponent()) != null) {
            container.add(vc, BorderLayout.CENTER);
        }
        if ((cc = p.getControlPanelComponent()) != null) {
            container.add(cc, BorderLayout.SOUTH);
        }
        setVisible(Boolean.TRUE);
        try {
            wc.startDevice("vfw:WebCam Vista/Live! Cam Chat (VFW):0");
        } catch (WebCameraException ex) {
            System.out.println("Errior");
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == start) {
            try {
                wc.startDevice("vfw:WebCam Vista/Live! Cam Chat (VFW):0");
            } catch (WebCameraException ex) {
            }
        } else {
            try {
                wc.stopDevice("vfw:WebCam Vista/Live! Cam Chat (VFW):0");
            } catch (WebCameraException ex) {
            }
        }
    }

    public static void main(String[] args) throws Exception {
        WebCamera cam = WebCameraSPI.getWebCamera();
        cam.initilize(new String[]{"vfw:WebCam Vista/Live! Cam Chat (VFW):0"});
        new MyJFrame("test", cam);
    }
}
